<template>
  <el-dialog title="物料产品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=false
             width="80%"
             center
  >

    <el-row :gutter="20">
      <!--物料数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="订单编号" prop="orderCode">
            <el-input
              v-model="queryParams.orderCode"
              placeholder="请输入订单编号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="到货日期" prop="materialDeadline">
            <el-date-picker
              v-model="queryParams.materialDeadline"
              type="date"
              placeholder="选择到货日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="物料名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入物料名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table border ref="itemTable" v-loading="loading" :data="itemList" @selection-change="handleSelectionChange" height="650">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column label="计划编号" width="180" align="center" key="planNumber" prop="planNumber"
                           v-if="columns[12].visible"/>
          <el-table-column label="订单编号" width="180" align="center" key="orderCode" prop="orderCode"
                           v-if="columns[0].visible"/>
          <el-table-column label="采购员" align="center" key="buyer" prop="buyer" v-if="columns[1].visible"/>
          <el-table-column label="物料编码" width="180" align="center" key="itemCode" prop="itemCode"
                           v-if="columns[2].visible"/>
          <el-table-column label="物料名称" align="center" key="itemName" prop="itemName" v-if="columns[3].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="规格型号" align="center" key="specification" prop="specification"
                           v-if="columns[4].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="单位" align="center" key="unitOfMeasure" prop="unitOfMeasure"
                           v-if="columns[5].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="所属分类" align="center" key="itemTypeName" prop="itemTypeName"
                           v-if="columns[6].visible"/>
          <el-table-column
            v-for="col in columnsConfig"
            :key="col.prop"
            :label="col.label"
            :prop="col.prop"
            :width="col.width"
            :align="col.align"
            v-if="col.show">
          </el-table-column>
          <el-table-column label="客户" align="center" key="clientName" prop="clientName" v-if="columns[11].visible"/>
          <!--          <el-table-column label="未到货数量" align="center" prop="notArrivedQuantity" v-if="columns[11].visible" width="160"/>-->
        </el-table>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getOrderDetails} from "@/api/mes/cg/arrived";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";
export default {
  name: "CgOrderDetailSingle",
  props: {
    vendorId: null,
    arrivedQuantity:{}
  },
  data() {
    return {
      loading: false,
      showFlag: false,
      selectedRows: undefined,
      selectedRows1: [],
      selectedRows2: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      queryParams: {
        vendorId: this.vendorId,
        orderCode: undefined,
        materialDeadline: undefined,
        itemName: undefined,
      },
      // 列信息
      columns: [
        {key: 0, label: `订单编码`, visible: true},
        {key: 1, label: `采购员`, visible: true},
        {key: 2, label: `物料/产品编码`, visible: true},
        {key: 3, label: `物料/产品名称`, visible: true},
        {key: 4, label: `规格型号`, visible: true},
        {key: 5, label: `单位`, visible: true},
        {key: 6, label: `物料分类`, visible: true},
        {key: 7, label: `品牌`, visible: true},
        {key: 8, label: `颜色`, visible: true},
        {key: 9, label: `技术要求`, visible: true},
        {key: 10, label: `型号`, visible: true},
        {key: 11, label: `客户`, visible: true},
        /*{key: 11, label: `未到货数量`, visible: true},*/
        {key: 12, label: `采购计划号`, visible: true}
      ],
      columnsConfig:[],
    };
  },
  created() {
    /*this.getList();*/
    this.getItemBase();
  },
  watch: {
    vendorId(newVal) {
      if (newVal) {
        this.queryParams.vendorId = newVal;
        this.getList();
      }
    }
  },
  methods: {
    init() {
      this.showFlag = true;
      this.selectedRows = [];
      this.selectedRows2 = [];
      this.getList();
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      getOrderDetails(this.queryParams).then(response => {
        if(response.data){
          response.data.forEach(item => {
            if (this.arrivedQuantity[item.orderDetailId]) {
              item.notArrivedQuantity =item.notArrivedQuantity+this.arrivedQuantity[item.orderDetailId];
            }
          })
        }
          this.itemList = response.data;
          this.loading = false;
          this.$nextTick(function () {
            if (this.selectedRows1.length > 0) {
              this.selectedRows1.forEach(row => {
                let data = this.itemList.find(item => item.orderDetailId == row.orderDetailId);
                if (data) {
                  this.$refs.itemTable.toggleRowSelection(data);
                } else {
                  row.notArrivedQuantity = row.arrivedQuantity;
                  this.selectedRows2.push(row);
                }

              })
            }
          })
        }
      );
    },
    handleSelectionChange(val) {
      this.selectedRows = val;
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRows == null || this.selectedRows.length == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.selectedRows = this.selectedRows.concat(this.selectedRows2);
      this.selectedRows.forEach(row => {
        row.iqcCheck = 'Y';
      })
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.itemName=null;
      this.queryParams.orderCode=null;
      this.queryParams.materialDeadline=null;
      this.handleQuery();
    },

  },
};
</script>
